<?php
namespace app\Models;
require_once "NotORM.php";

class RoleModel {
	//查询数据库动作
	public function searchRoleModel() {

		//链接数据库
		$conn = mysqli_connect("localhost", "root", "123456", "rolesystem");
		//MySQL查询语句
		$sql = "SELECT * FROM rolesystem_role;";
		//绑定数据库查询结果
		$result = mysqli_query($conn, $sql);
		$arr = mysqli_fetch_all($result);
		//新建返回对象
		$ret = new \stdClass();
		$ret -> data = array();
		//遍历数据库返回对象绑定Model返回内容

		for ($i = 0; $i < count($arr); $i++) {
			$obj = new \stdClass();
			foreach ($arr[$i] as $key => $val) {
				$obj -> $key = $val;
			}
			array_push($ret -> data, $obj);
		}

		return $ret;

	}

	//删除数据动作
	public function deleteRoleModel($id) {
		//链接数据库
		$conn = mysqli_connect("localhost", "root", "123456", "rolesystem");
		$sql = "DELETE FROM rolesystem_role WHERE roleid=?;";
		// 预处理及绑定
		$stmt = $conn -> prepare($sql);
		//绑定参数
		$stmt -> bind_param("s", $id);
		//执行
		$stmt -> execute();
		//绑定数据库返回结果
		$result = Array();
		if ($stmt -> affected_rows) {
			$result["error"] = 0;
			$result["message"] = "信息刪除成功";
		} else {
			$result["error"] = 1;
			$result["message"] = "信息刪除失敗";
		}
		//关闭预处理
		$stmt -> close();
		//返回信息
		return $result;

	}

	//修改数据动作
	public function modifyRoleModel($role) {
		$pdo = new \PDO("mysql:host=localhost;dbname=rolesystem", "root", "123456");
		$pdo -> exec("SET NAME 'utf-8'");
		$db = new \NotORM($pdo);
		return $db -> rolesystem_role("roleid=?", $role["roleid"]) -> update($role);
	}

	//增加数据动作
	public function insertRoleModel($role) {
		$pdo = new \PDO("mysql:host=localhost;dbname=rolesystem", "root", "123456");
		$pdo -> exec("SET NAME 'utf-8'");
		$db = new \NotORM($pdo);
		return $db -> rolesystem_role() -> insert($role);
	}

}
?>